#ifndef __APP_BUFFER_H__
#define __APP_BUFFER_H__

#include "pthread.h"
//小缓冲区
typedef struct {
    unsigned char *ptr;  //缓冲区指针
    int size;   //缓冲区大小
    int len;    //缓冲区数据长度
}SubBuffer;

//缓冲区
typedef struct {
    SubBuffer  *sub_buffers[2];      //包含读写缓冲区
    int read_index;                 //读缓冲区索引
    int write_index;                //写缓冲区索引
    pthread_mutex_t read_lock;      //读缓冲区锁
    pthread_mutex_t write_lock;     //写缓冲区锁
}Buffer;

/**
 * @brief 初始化缓冲区
 * 
 * @param size  缓冲区大小
 * @return Buffer*  缓冲区指针
 */
Buffer *app_buffer_init(int size);

/**
 * @brief 销毁缓冲区
 * 
 * @param buffer    缓冲区指针
 */
void app_buffer_destroy(Buffer *buffer);

/**
 * @brief 写入数据
 * 
 * @param buffer    缓冲区指针
 * @param data      数据指针
 * @param len       数据长度
 * @return int  
 */
 int app_buffer_write(Buffer *buffer, char *data, int len);

/**
 * @brief 读取数据
 * 
 * @param buffer    缓冲区指针
 * @param data_buf  数据缓冲区指针
 * @param buf_len   数据缓冲区大小
 * @return int 
 */
 int app_buffer_read(Buffer *buffer, char *data_buf, int buf_len);

#endif /* __APP_BUFFER_H__ */